/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.scs.model;

import com.wisnu.scs.dao.DbBarangDao;
import com.wisnu.scs.dao.DbBarangRekapDao;
import com.wisnu.scs.entity.DbBarang;
import com.wisnu.scs.entity.DbBarangRekap;
import com.wisnu.scs.error.ErrorMessage;
import com.wisnu.scs.event.DbBarangListener;
import com.wisnu.scs.util.HibernateUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Wisnu Wardoyo
 */
public class DbBarangModel {

    private Integer id;
    private String namaBarang;
    private Integer stock;
    private String searchText;

    private Date firstDate;
    private List<Date> listDate;

    private DbBarangListener listener;

    public void setListener(DbBarangListener listener) {
        this.listener = listener;
    }

    public String getSearchText() {
        return searchText;
    }

    public void setSearchText(String searchText) {
        this.searchText = searchText;
        fireOnReset(this);
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
        fireOnReset(this);
    }

    public String getNamaBarang() {
        return namaBarang;
    }

    public void setNamaBarang(String namaBarang) {
        this.namaBarang = namaBarang;
        fireOnReset(this);
    }

    public Integer getStock() {
        return stock;
    }

    public void setStock(Integer stock) {
        this.stock = stock;
        fireOnReset(this);
    }

    public Date getFirstDate() {
        return firstDate;
    }

    public void setFirstDate(Date firstDate) {
        this.firstDate = firstDate;
    }

    public List<Date> getListDate() {
        return listDate;
    }

    public void setListDate(List<Date> listDate) {
        this.listDate = listDate;
    }

    protected void fireOnReset(DbBarangModel barangModel) {
        if (listener != null) {
            listener.onReset(barangModel);
        }
    }

    protected void fireOnInsert(DbBarang barang) {
        if (listener != null) {
            listener.onInsert(barang);
        }
    }

    protected void fireOnUpdate(DbBarang barang) {
        if (listener != null) {
            listener.onUpdate(barang);
        }
    }

    protected void fireOnDelete(DbBarang barang) {
        if (listener != null) {
            listener.onDelete(barang);
        }
    }

    protected void fireOnSearch(List<DbBarang> barang) {
        if (listener != null) {
            listener.onSearch(barang);
        }
    }

    protected void fireOnSearchByDate(List<DbBarang> barang, Date date) {
        if (listener != null) {
            listener.onSearchByDate(barang, date);
        }
    }

    protected void fireOnSearchByDateRange(List<DbBarang> barang, List<Date> dateList) {
        if (listener != null) {
            listener.onSearchByDateRange(barang, dateList);
        }
    }

    protected void fireOnSearch(List<DbBarang> barang, List<DbBarangRekap> barangRekap) {
        if (listener != null) {
            listener.onSearch(barang, barangRekap);
        }
    }

    public void insertBarang() throws ErrorMessage {
        DbBarangDao barangDao = HibernateUtil.getBarangDao();

        DbBarang barang = new DbBarang();
        barang.setNamaBarang(namaBarang);
        barang.setStock(stock);

        barangDao.insertBarang(barang);
        fireOnInsert(barang);
    }

    public void updateBarang() throws ErrorMessage {
        DbBarangDao barangDao = HibernateUtil.getBarangDao();

        DbBarang barang = new DbBarang();
        barang.setIdBarang(id);
        barang.setNamaBarang(namaBarang);
        barang.setStock(stock);

        barangDao.updateBarang(barang);
        fireOnUpdate(barang);
    }

    public void deleteBarang() throws ErrorMessage {
        DbBarangDao barangDao = HibernateUtil.getBarangDao();

        DbBarang barang = new DbBarang();
        barang.setIdBarang(id);
        barang.setNamaBarang(namaBarang);
        barang.setStock(stock);

        barangDao.deleteBarang(barang);
        fireOnDelete(barang);
    }

    public void resetForm() {
        setId(0);
        setNamaBarang("");
        setStock(0);
        setSearchText("");
    }

    public void searchBarang() throws ErrorMessage {
        DbBarangDao barangDao = HibernateUtil.getBarangDao();

        fireOnSearch(barangDao.selectBarangByName(searchText));
    }

    public void searchBarangByDate() throws ErrorMessage {
        DbBarangDao barangDao = HibernateUtil.getBarangDao();

        fireOnSearchByDate(barangDao.selectBarangByName(searchText), firstDate);
    }

    public void searchBarangByMonth() throws ErrorMessage {
        DbBarangDao barangDao = HibernateUtil.getBarangDao();

        fireOnSearchByDateRange(barangDao.selectBarangByName(searchText), listDate);
    }

    public void searchBarangByDateRange() throws ErrorMessage {
        DbBarangDao barangDao = HibernateUtil.getBarangDao();

        fireOnSearchByDateRange(barangDao.selectBarangByName(searchText), listDate);
    }

    public void searchBarangByDate2() throws ErrorMessage {
        DbBarangRekapDao barangRekapDao = HibernateUtil.getBarangRekapDao();

        List<DbBarang> listBarang = new ArrayList();
        List<DbBarangRekap> listBarangRekap = barangRekapDao.selectByDate(firstDate);

        for (DbBarangRekap barangRekap : listBarangRekap) {
            listBarang.add(barangRekap.getBarang());
        }

        fireOnSearch(listBarang, listBarangRekap);
    }

    public void searchBarangByDateRange2() throws ErrorMessage {
        DbBarangRekapDao barangRekapDao = HibernateUtil.getBarangRekapDao();

        List<DbBarang> listBarang = new ArrayList();
        List<DbBarangRekap> listBarangRekap = barangRekapDao.selectByDateRange(listDate);

        for (DbBarangRekap barangRekap : listBarangRekap) {
            listBarang.add(barangRekap.getBarang());
        }

        fireOnSearch(listBarang, listBarangRekap);
    }

}
